package com.exam.controller;

import com.exam.entity.Examination;
import com.exam.entity.JsonModel;
import com.exam.entity.Student;
import com.exam.service.ExaminationService;
import com.exam.service.impl.ExaminationServiceImpl;
import com.exam.util.FileUtils;
import com.exam.util.ZLConstants;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@RestController
public class ExaminationController {
    
    @Resource
    private ExaminationService examinationService;

    @RequestMapping("/examination")
    public Object exam(String op,String ename,String eids,String pageNum,String pageSize,HttpSession session){
        if(op!=null && !"".equals(op)){
            if("all".equals(op)){
                List<Map<String,Object>> exams = examinationService.getAllExaminations();
                return exams;
            }else if("del".equals(op)){
                int result = 0;
                String[] strs = eids.split(",");
                for (String eid : strs){
                    if(examinationService.delExamination(Integer.parseInt(eid))>0){
                        result = 1;
                    }
                }
                return result;
            }else if("getByPage".equals(op)){
                if(Integer.parseInt(pageNum) > examinationService.getCountByExamination()/Integer.parseInt(pageSize)+1){//当前页码不能大于最大页码
                    return null;
                }
                List<Map<String,Object>> exams = examinationService.getExaminationsByPage(pageNum,pageSize);
                return exams;
            }else if("searchExam".equals(op)){
                List<Map<String,Object>> exams = examinationService.getExaminationByName(ename);
                return exams;
            }
        }
        return null;
    }

    @RequestMapping("/addExam")
    public int addExamination(Examination examination, MultipartFile epic1,HttpServletRequest request){
        if(epic1.getSize()<=0){
            examination.setEpic("/image/zanwu.jpg");
        }else{
            examination.setEpic("/image/"+epic1.getOriginalFilename());
            FileUtils.saveFile(epic1,request);
        }
        return examinationService.addExamination(examination);
    }

    @RequestMapping("/modExam")
    public int modExamination(Examination examination, MultipartFile epic2,HttpServletRequest request){
        if(epic2.getSize()<=0){
            examination.setEpic("/image/zanwu.jpg");
        }else{
            examination.setEpic("/image/"+epic2.getOriginalFilename());
            FileUtils.saveFile(epic2,request);
        }
        return examinationService.editExamination(examination);
    }

    public void setExaminationService(ExaminationService examinationService) {
        this.examinationService = examinationService;
    }
}
